/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sarco.bwi;

import com.sarco.bwi.lib.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iktsup
 */
public class Main {
    public static void main(String[] args) {
        boolean canTerminate = false;
        
        try {
            System.out.println("STARTING!");
            Config config = new Config();
            config.loadConfig(Config.DEFAULT_CONFIG);

            Node node = new Node();
            node.init(config);
            node.waitForJob();
            node.startJob();

            node.getOut().write(Node.NODE_DONE.getBytes());

            while (!canTerminate) {
                String s = "";
                byte[] bIn = new byte[512];
                node.getIn().read(bIn);
                for (int y = 0; y < bIn.length; y++) {
                    s += (char) bIn[y];
                }

                if (s.startsWith(Node.MASTER_TERMINATE))
                    canTerminate = true;
            }

            System.out.println("TERMINATING!");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
